/* test2.c
 *
 * Copyright (C) 2007 Renato Araujo Oliveira Filho <renatox@gmail.com>
 * Copyright (C) 2007 Andre Moreira Magalhaes <andrunko@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <goocanvas.h>

#include "gtktransition.h"

static gboolean on_delete_event      (GtkWidget      *window,
                                      GdkEvent       *event,
                                      gpointer        unused_data);
static void     on_button_click      (GtkButton      *btn,
                                      GtkTimeline    *timeline);
static void     on_frame_changed_cb  (GtkTimeline    *line,
                                      gdouble         progress,
                                      GtkRange       *range);
static void     set_opacity_func     (GtkTransitionState *state,
                                      const gchar        *prop_name,
                                      gdouble             progress,
                                      GValue             *result);



int
main (int argc, char *argv[])
{
  GtkWidget *hscale;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *btn;
  GtkWidget *window, *scrolled_win, *canvas;
  GooCanvasItem *root, *item;
  GtkTransition *transition;
  GtkTransitionState *state1, *state2;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
  gtk_widget_show (window);
  g_signal_connect (window, "delete_event",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
                                       GTK_SHADOW_IN);
  gtk_widget_show (scrolled_win);

  canvas = goo_canvas_new ();
  gtk_widget_set_size_request (canvas, 640, 480);
  goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 1000, 1000);
  gtk_widget_show (canvas);

  vbox = gtk_vbox_new (FALSE, 6);
  hbox = gtk_hbox_new (FALSE, 6);

  btn = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);

  hscale = gtk_hscale_new_with_range (0.0, 100.0, 1.0);

  gtk_box_pack_start (GTK_BOX (hbox), btn, FALSE, TRUE, 6);
  gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 6);

  gtk_container_add (GTK_CONTAINER (scrolled_win), canvas);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show_all (vbox);

  root = goo_canvas_get_root_item (GOO_CANVAS (canvas));

  transition = gtk_transition_new (1000);

  item = goo_canvas_rect_new (root, 0, 0, 100, 100, 
                              "fill-color", "red", 
                              "fill-color-rgba", 10, NULL);

  goo_canvas_item_add_child (root, item, -1);

  state1 = gtk_transition_state_new (G_OBJECT (item));
  gtk_transition_state_set (state1,
                            "x", 20.0, 0.0,
                            "y", 20.0, 0.0,
                            NULL);
  gtk_transition_add_state (transition,
                            state1,
                            NULL,
                            0.5);
  g_object_unref (state1);

  state2 = gtk_transition_state_new (G_OBJECT (item));
  gtk_transition_state_set (state2,
                            "width", 100.0, 400.0,
                            "height", 100.0, 400.0,
                            NULL);
  gtk_transition_state_set_full (state2,
                                 "fill-color-rgba", G_TYPE_UINT, 0, 255,
                                 (GtkTransitionStateProgressFunc) set_opacity_func,
                                 NULL);
  gtk_transition_add_state (transition,
                            state2,
                            NULL /* state1 */,
                            1.0);
  g_object_unref (state2);

  g_signal_connect (btn, "clicked",
                    G_CALLBACK (on_button_click),
                    transition);

  g_signal_connect (transition,
                    "frame",
                    G_CALLBACK (on_frame_changed_cb),
                    hscale);

  gtk_main ();

  return 0;
}

static gboolean
on_delete_event (GtkWidget *window,
                 GdkEvent  *event,
                 gpointer   unused_data)
{
  gtk_main_quit ();
}

static void
on_button_click (GtkButton   *button,
                 GtkTimeline *timeline)
{
  gtk_timeline_set_direction (timeline, GTK_TIMELINE_DIRECTION_FORWARD);
  gtk_timeline_start (timeline);
}

static void
on_frame_changed_cb (GtkTimeline *timeline,
                     gdouble      progress,
                     GtkRange    *range)
{
  if (gtk_timeline_is_running (timeline))
    gtk_range_set_value (range, progress * 100);

  if (progress == 1.0)
    {
      gtk_timeline_set_direction (timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
      gtk_timeline_rewind (timeline);
    }
}

static void
set_opacity_func (GtkTransitionState *state,
                  const gchar        *prop_name,
                  gdouble             progress,
                  GValue             *v_result)
{
    GValue *v_start;
    GValue *v_end;
    guint32 start;
    guint32 end;
    guint32 result;

    gtk_transition_state_get_range (state, prop_name, &v_start, &v_end);
    start = g_value_get_uint (v_start);
    end = g_value_get_uint (v_end);

    result = (start + (end - start) * progress);
    g_value_init (v_result, G_TYPE_INT);
    g_value_set_int (v_result,
                     0xff000000 + result);
}
